package bma.common.netty.webserver.common;

import java.util.HashMap;
import java.util.Map;

import bma.common.langutil.ai.stack.AIStack;
import bma.common.netty.webserver.RequestContext;
import bma.common.netty.webserver.RequestDispatcher;
import bma.common.netty.webserver.RequestProcessor;

public class RequestDispatchGroup extends RequestDispatcher {

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(RequestDispatchGroup.class);

	protected Map<String, RequestProcessor> processors;

	public Map<String, RequestProcessor> getProcessors() {
		return processors;
	}

	public void setProcessors(Map<String, RequestProcessor> processors) {
		this.processors = processors;
	}

	public void add(String subPath, RequestProcessor p) {
		if (this.processors == null) {
			this.processors = new HashMap<String, RequestProcessor>();
		}
		this.processors.put(subPath, p);
	}

	@Override
	public boolean dispatch(AIStack<Boolean> stack, RequestContext ctx)
			throws Exception {
		String name = ctx.getPath();
		RequestProcessor pro = this.processors.get(name);
		if (pro != null) {
			if (log.isDebugEnabled()) {
				log.debug("pathgroup dispatch {} => {}", name, pro);
			}
			return dispatchOrProcess(stack, pro, ctx);
		}
		return false;
	}

}
